﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDto.UserDtos;
using CozeDomain.IServices;
using CozeDto.AdminDtos;
using D.UtilFreesqlOrm;
using System.Security.Cryptography;
using D.Util.UtilSecurity;
using CozeDomain.Services;
using CozeDto.ProjectDtos;

namespace CozeWebApi
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class UserController : BaseApiController
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddUser"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddUser reqAddUser)
        {
            reqAddUser.CreateUser = Account;
            return await _userService.AddUserAsync(reqAddUser);
        }

        /// <summary>
        /// 删除，多个逗号隔开
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _userService.DeleteUserAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateUser"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateUser reqUpdateUser)
        {
            reqUpdateUser.Id = id;
            reqUpdateUser.UpdateUser = Account;
            return await _userService.UpdateUserAsync(reqUpdateUser);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdatePwd"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> UpdatePwd(string id, [FromBody] ReqUpdateUserPwd reqUpdatePwd)
        {
            reqUpdatePwd.Id = id;
            reqUpdatePwd.UpdateUser = Account;
            return await _userService.UpdatePwdAsync(reqUpdatePwd);
        }

        /// <summary>
        /// 重设密码为123456
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        //[Authorize]
        //public async Task<ApiResult<int>> ResetPwd(string id)
        //{
        //    var pwd = MD5EncryptExtensions.ToMd5Hash("123456");
        //    var reqUpdatePwd = new ReqUpdateUserPwd()
        //    {

        //        Id = id,
        //        UpdateUser = Account,
        //        Pwd = pwd,
        //        PasswordConfirm = pwd,
        //    };
        //    return await _userService.UpdatePwdAsync(reqUpdatePwd);
        //}

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqChangeStatus"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> ChangeStatus(string id, [FromBody] ReqChangeStatus reqChangeStatus)
        {
            reqChangeStatus.Id = id;
            reqChangeStatus.UpdateUser = Account;
            return await _userService.ChangeStatusAsync(reqChangeStatus);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepUserInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _userService.GetUserAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepUserInfo>>> List()
        {
            return await _userService.QueryUserListAsync();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepUserInfo>>> Pages([FromBody] ReqQueryUserPage reqQueryUserPage)
        {
            return await _userService.QueryUserPagesAsync(reqQueryUserPage);
        }
    }
}
